package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.AnswerDao;
import com.zretc.support.dao.OnlineDao;
import com.zretc.support.dao.ScoreDao;
import com.zretc.support.entity.Answer;
import com.zretc.support.entity.Online;
import com.zretc.support.entity.Score;
import com.zretc.support.entity.ScoreToPart;
import com.zretc.support.service.OnlineService;
import com.zretc.support.service.ScoreService;
import com.zretc.support.tool.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    ScoreDao sd;
    @Autowired
    AnswerDao ad;

    @Override
    public Page<Score> selectByIdOrName(String keywords, Integer page, Integer limit) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<Score> pd = sd.selectByIdOrName(keywords, pageable);
        return pd;

    }

    @Override
    public int addScore(Score score) {
        Score score1 = sd.save(score);
        if (score1 != null) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updScore(int answerId, double score, int examId, String userId, int scoreId) {
        //修改单个题目的学生得分
        Answer answer = ad.getOne(answerId);
        answer.setSocre(score);
        ad.save(answer);
        //修改总分
        double totalScore = getTotalScore(examId, userId);
        Score s = sd.getOne(scoreId);
        s.setTotalScore(totalScore);
        sd.save(s);
        return 1;
    }

    @Override
    public int delScoreById(Integer scoreId) {
        try {
            sd.deleteById(scoreId);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    /**
     * 计算 某个同学 某场考试 的总分
     *
     * @param examId
     * @param userId
     * @return
     */
    @Override
    public double getTotalScore(int examId, String userId) {
        double totalScore = 0;
        // 获取 某场考试的 某个学生的所有答题信息
        List<Answer> answerList = ad.findAnswerByExamIdAndUserId(examId, userId);
        //将分数全部加起来
        for (Answer answer : answerList) {
            totalScore += answer.getSocre();
        }
        return totalScore;
    }

    /**
     * 分页+模糊（每场考试的 avg、max、min）
     * @param keywords
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Page<ScoreToPart> getPart(String keywords, Integer page, Integer limit) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<ScoreToPart> pd = sd.getPart(keywords, pageable);
        return pd;
    }

    /**
     * 获取某场考试的 所有学生的成绩
     * 用于图表分析
     * @param examId
     * @return
     */
    @Override
    public List<Score> getScoresByExamId(long examId) {
        return sd.getScoresByExamId(examId);
    }


    @Override
    public PageData<Score> getScoreByUserId(String userId, String examName, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Score> pg = sd.getScoreByUserId(userId,examName, pageable);
        PageData<Score> pageData = new PageData<>();
        pageData.setRecords(pg.getContent());
        pageData.setCurrent(pg.getNumber());
        pageData.setTotal((int) pg.getTotalElements());
        pageData.setSize(pg.getSize());
        pageData.setCode(200);
        pageData.setMsg("查询成功！");
        return pageData;
    }

    /**
     * 只能允许有 唯一的examId、userId组合
     * @param examId
     * @param userId
     * @return
     */
    @Override
    public Score getScoresByExamIdAndUerId(int examId, String userId) {
        return sd.getScoresByExamIdAndUerId((long)examId,userId);
    }

    @Override
    public int updScore1(Score score) {
        sd.save(score);
        return 1;
    }

    /**
     * 模糊查询获取某学生的成绩
     * @param userId
     * @param keywords
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Page<Score> getScoresByUserId(String userId, String keywords, int page, int limit) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<Score> pd = sd.getScoresByUserId(userId,keywords,pageable);
        return pd;
    }



}
